package cn.wolfcode.wolf2world.service.impl;

import cn.wolfcode.wolf2world.domain.StrategyCatalog;
import cn.wolfcode.wolf2world.query.QueryObject;
import cn.wolfcode.wolf2world.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2world.service.StrategyCatalogService;
import cn.wolfcode.wolf2world.service.StrategyService;
import cn.wolfcode.wolf2world.utils.DBHelper;
import cn.wolfcode.wolf2world.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 杨兴健
 * @Date 2020/8/9 20:09
 */
@Service
public class StrategyCatalogServiceImpl implements StrategyCatalogService {

    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;
    @Autowired
    private StrategyService strategyService;
    @Autowired
    private MongoTemplate mongoTemplatep;


    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        strategyCatalogRepository.insert(strategyCatalog);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        strategyCatalogRepository.deleteById(id);
    }

    @Override
    public StrategyCatalog get(String id) {
        return strategyCatalogRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyCatalog> list() {
        return strategyCatalogRepository.findAll();
    }

    @Override
    public Page<StrategyCatalog> query(QueryObject qo) {
        Query query = new Query();

        return DBHelper.getPage(mongoTemplatep, query, qo, StrategyCatalog.class, Sort.Direction.ASC, "sequence");
    }

    /**
     * 添加或更新
     */
    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        if (StringUtils.hasLength(strategyCatalog.getId())) {
            this.update(strategyCatalog);
        } else {
            this.save(strategyCatalog);
        }
    }

    /**
     * 查询攻略分类列表
     *
     * @return
     */
    @Override
    public List<CatalogVO> queryCatalogVO() {
        //TODO 聚合查询没有消化
        List<CatalogVO> list = new ArrayList<>();
        //聚合查询
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
            Aggregation.group("destName")
                        .push("name").as("names")
                        .push("id").as("ids")

        );
        AggregationResults<Map> result = mongoTemplatep.aggregate(agg, Map.class);
        //将结果转化成list<map>   map描述一行数据
        List<Map> datas = result.getMappedResults();
        for (Map data : datas) {
            CatalogVO vo = new CatalogVO();
            //id
            vo.setDestName(data.get("_id").toString());
            //攻略分类的名称集合
            List<Object> names = (List<Object>) data.get("names");
            //攻略分类的ids集合
            List<Object> ids = (List<Object>) data.get("ids");
            //攻略分类对象
            List<StrategyCatalog> catalogList = new ArrayList<>();
            for (int i = 0; i < names.size(); i++) {
                StrategyCatalog sc = new StrategyCatalog();
                String name = names.get(i).toString();
                String id = ids.get(i).toString();
                sc.setId(id);
                sc.setName(name);
                catalogList.add(sc);
            }
            vo.setCatalogs(catalogList);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<StrategyCatalog> queryCatalogByDeatId(String destId) {
        return strategyCatalogRepository.findByDestId(destId);
    }
}



